package com.linked.store.box.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.store.box.bean.param.AddBoxInfoParam;
import com.linked.store.box.bean.param.BoxPageParam;
import com.linked.store.box.bean.param.ThingEnchaseParam;
import com.linked.store.box.bean.po.BoxInfoPO;
import com.linked.store.box.bean.po.BoxThingPO;
import com.linked.store.box.bean.vo.BoxPageVO;
import com.linked.store.box.mapper.IBoxInfoMapper;
import com.linked.store.box.mapper.IBoxThingsMapper;
import com.linked.store.box.service.IBoxHandleService;
import com.linked.store.label.bean.vo.BoxPageLabelVO;
import com.linked.store.label.mapper.ILabelInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/3/17 10:56
 * @description : desc
 */
@Slf4j
@Service
public class BoxHandleServiceImpl implements IBoxHandleService {

    private final IBoxInfoMapper boxInfoMapper;

    private final IBoxThingsMapper boxThingsMapper;

    private final ObjectMapper mapper;

    private final ILabelInfoMapper labelInfoMapper;

    @Autowired
    public BoxHandleServiceImpl(IBoxInfoMapper boxInfoMapper, ObjectMapper mapper, IBoxThingsMapper boxThingsMapper,ILabelInfoMapper labelInfoMapper) {
        this.boxInfoMapper = boxInfoMapper;
        this.boxThingsMapper = boxThingsMapper;
        this.mapper = mapper;
        this.labelInfoMapper = labelInfoMapper;
    }

    @Override
    public boolean addBoxInfo(AddBoxInfoParam param) throws Exception {
        BoxInfoPO temp = new BoxInfoPO();
        temp.setBoxName(param.getBoxName());
        temp.setBoxType(param.getBoxType());
        temp.setBoxRemarks(param.getBoxRemarks());
        temp.setBoxPosition(param.getBoxPosition());
        return boxInfoMapper.insert(temp) > 0;
    }

    @Override
    public boolean thingEnchase(ThingEnchaseParam param) throws Exception {
        QueryWrapper<BoxThingPO> wrapper = new QueryWrapper<>();
        wrapper.eq("thing_id", param.getThingId());
        wrapper.eq("box_id", param.getBoxId());
        int count = boxThingsMapper.selectCount(wrapper);
        if (count > 0) {
            return true;
        }
        BoxThingPO temp = new BoxThingPO();
        temp.setBoxId(param.getBoxId());
        temp.setThingId(param.getThingId());
        return boxThingsMapper.insert(temp) > 0;
    }

    @Override
    public BoxInfoPO findBoxInfo(String boxId) throws Exception {
        return boxInfoMapper.selectById(boxId);
    }

    @Override
    public IPage<BoxPageVO> queryBoxPage(BoxPageParam para) throws Exception {
        IPage<BoxPageVO> boxPage = boxInfoMapper.queryBoxPage(para);
        boxPage.getRecords().stream().forEach(x->{
            List<BoxPageLabelVO> labels = labelInfoMapper.queryBoxLabels(x.getBoxId());
            x.setLabels(labels);
        });
        return boxPage;
    }
}
